package ${package.Controller};



import org.springframework.web.bind.annotation.RequestMapping;
import com.guodi.core.tool.api.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;


import com.guodi.common.exception.BaseRuntimeException;
<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.stereotype.Controller;
</#if>
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>
<#-- swagger的注解 -->
<#if swagger2>
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;
</#if>
<#-- 自定义代码要用的 -->
<#if cfg[table.name]["firstLowerServiceName"]??>
import javax.annotation.Resource;
import ${package.Service}.${table.serviceName};
import ${package.Entity}.${entity};
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.NotBlank;
import java.util.Arrays;
import java.util.List;
</#if>


/**
* ${table.comment!} 前端控制器
* ${cfg["annotationAuthor"]} ${author}
* ${cfg["annotationDate"]} ${date}
*/
@Slf4j
@Validated
<#if restControllerStyle>
@RestController
<#else>
@Controller
</#if>
@RequestMapping("<#if package.ModuleName??>/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
<#if swagger2>
@Api(value = "API - ${table.controllerName}", tags = "${table.comment!table.controllerName}")
</#if>
<#if kotlin>
class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
<#else>
<#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass} {
<#else>
public class ${table.controllerName} {
</#if>

    <#if cfg[table.name]["firstLowerServiceName"]??>
    @Resource
    private ${table.serviceName} ${cfg[table.name]["firstLowerServiceName"]};


    /**
    * 新增 ${table.comment!} 记录
    * ${cfg["annotationAuthor"]} ${author}
    * ${cfg["annotationDate"]} ${date}
    */
    @PostMapping("/save")
        <#if swagger2>
    @ApiOperation(value = "新增 ${table.comment!} 记录")
        </#if>
    @Transactional(rollbackFor = Exception.class)
    public R<${entity}> save(@Validated ${entity} ${cfg[table.name]["firstLowerEntityName"]}){
        // 新增
        boolean save = this.${cfg[table.name]["firstLowerServiceName"]}.save(${cfg[table.name]["firstLowerEntityName"]});
        if (save){
            return R.data(this.${cfg[table.name]["firstLowerServiceName"]}.getById(${cfg[table.name]["firstLowerEntityName"]}.getId()));
        }else{
            throw new BaseRuntimeException("新增失败");
        }
    }

    /**
    * 修改 ${table.comment!} 记录
    * ${cfg["annotationAuthor"]} ${author}
    * ${cfg["annotationDate"]} ${date}
    */
    @PostMapping("/update")
        <#if swagger2>
    @ApiOperation(value = "修改 ${table.comment!} 记录")
        </#if>
    @Transactional(rollbackFor = Exception.class)
    public R<${entity}> update(@Validated ${entity} ${cfg[table.name]["firstLowerEntityName"]}){
        // 修改
        ${entity} oldEntity = this.${cfg[table.name]["firstLowerServiceName"]}.getById(${cfg[table.name]["firstLowerEntityName"]}.getId());
        LambdaUpdateWrapper<${entity}> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.setEntity(oldEntity);
        boolean update = this.${cfg[table.name]["firstLowerServiceName"]}.update(${cfg[table.name]["firstLowerEntityName"]} ,updateWrapper);
        if (update){
            return R.data(this.${cfg[table.name]["firstLowerServiceName"]}.getById(${cfg[table.name]["firstLowerEntityName"]}.getId()));
        }else{
            throw new BaseRuntimeException("修改失败");
        }
    }


    /**
    * 删除 ${table.comment!} 记录
    * ${cfg["annotationAuthor"]} ${author}
    * ${cfg["annotationDate"]} ${date}
    */
    @PostMapping("/delete")
        <#if swagger2>
    @ApiOperation(value = "删除 ${table.comment!} 记录")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "ids", value = "主键id，如果多个，英文逗号隔开", required = true, dataType = "string", paramType = "query")
    })
        </#if>
    @Transactional(rollbackFor = Exception.class)
    public R<List<${entity}>> delete(@Validated @NotBlank(message = "请选择记录再进行删除") String ids){
        List<String> idList = Arrays.asList(ids.trim().split(","));
        List<${entity}> ${cfg[table.name]["firstLowerEntityName"] + "s"} = this.${cfg[table.name]["firstLowerServiceName"]}.listByIds(idList);
        boolean remove = this.${cfg[table.name]["firstLowerServiceName"]}.removeByIds(idList);
        if (remove){
            return R.data(${cfg[table.name]["firstLowerEntityName"] + "s"});
        }else{
            throw new BaseRuntimeException("删除失败");
        }
    }


    /**
    * 分页获取 ${table.comment!} 列表
    * ${cfg["annotationAuthor"]} ${author}
    * ${cfg["annotationDate"]} ${date}
    */
    @PostMapping("/page")
        <#if swagger2>
    @ApiOperation(value = "分页获取 ${table.comment!} 列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "size", value = "页面大小，默认10", dataType = "int", paramType = "query"),
        @ApiImplicitParam(name = "current", value = "当前页码，默认1",  dataType = "int", paramType = "query"),
    })
        </#if>
    public R<Page<${entity}>>page(<#if swagger2>@ApiIgnore</#if> Page page){
        Page page1 = this.${cfg[table.name]["firstLowerServiceName"]}.page(page);
        return R.data(page1);
    }



    /**
    * 获取 ${table.comment!} 详情
    * ${cfg["annotationAuthor"]} ${author}
    * ${cfg["annotationDate"]} ${date}
    */
    @PostMapping("/detail")
        <#if swagger2>
    @ApiOperation(value = "获取 ${table.comment!} 详情")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "主键id", required = true, dataType = "string", paramType = "query")
    })
        </#if>
    public R<${entity}> detail(@Validated @NotBlank(message = "请选择记录再进行查看详情") String id){
        ${entity} ${cfg[table.name]["firstLowerEntityName"]} = this.${cfg[table.name]["firstLowerServiceName"]}.getById(id);
        return R.data(${cfg[table.name]["firstLowerEntityName"]});
    }
    </#if>
}
</#if>
